<?php
/**
 * 员工列表
 */
apf_require_controller("HrAbstract");
apf_require_class("Const_Flow");

class Hr_Staff_ListController extends HrAbstractController {

    const PAGE_ROWS = 10; //每页显示10条
    const PAGE_NUM_SIZE = 10; //翻页工具条显示10个页码链接

    public function handle_request() {
        $params = $this->request->get_parameters();
        $page = intval($params['p']);
        $page = $page<1?1:$page;
        $offset = ($page-1)*self::PAGE_ROWS;

        $this->user_bll = new Bll_Hr_User();
        $bll = new Bll_Hr_Property();
        $rel_bll = new Bll_Hr_UserRelation();
        //从职位列表过来的
        if($params['jobid']){
            $params = $this->_build_get_data($params['jobid']);
            $this->user_bll->set_job_id($params['jobid']);

        }
        $params['search_type']= empty($params['search_type']) ? 1 : intval($params['search_type']);

        //更改职位和上级
        if(in_array($params['optionsRadios'],array(1,2)) && $_POST){
            if(empty($params['user_ids']) || empty($params['job_id'])){
                $this->show_tips('ERROR_NO_SELECT');
            }

            if($params['optionsRadios']==1){//更改职位
                $rel_bll->update_multi_user_work($params['user_ids'],$params['job_id']);
            }elseif($params['optionsRadios']==2){//更改直属上级
                $rel_bll->update_multi_user_parent($params['user_ids'],$params['user_id'],$params['job_id']);
            }
        }

        $selectdata['citylist'] = $bll->get_sort_city_list();
        $selectdata['business'] = $bll->get_business_list();
        $selectdata['function'] = $bll->get_function_list();

        $where = $this->_build_where($params);
        $where['role_list'] = empty($_SESSION['role_list'][Const_Flow::ROLE_HR]) ? array() : $_SESSION['role_list'][Const_Flow::ROLE_HR];
        //从离职转岗过来的信息
        $user_data = $this->_build_user_data($params);
        if(empty($user_data) && $params['parent_id']){
            $user_list['count'] = 0;
            $user_list['user_list'] = array();
        }else{
            if(!empty($user_data)){
                $this->user_bll->set_user_ids($user_data);
            }
            $user_list = $this->user_bll->get_work_user_list($where,self::PAGE_ROWS,$offset);
        }

        $data['user_count'] = $user_list['count'];
        $data['user_list'] = $user_list['datalist'];

        $uri_params = array('p' => $page);
        $uri_params = array_merge($uri_params,$where);//合并查询条件
        //print_r($uri_params);
        unset($uri_params['job_status']);
        unset($uri_params['role_list']);

        $url_suffix = "";
        $url_prefix = Util_BaseUrls::staff_list_url();
        $total = $data['user_count'];
        if($total > self::PAGE_ROWS){
            $data['multipage'] = Util_MultiPage::multiPage($url_prefix, $uri_params, $url_suffix, $total, self::PAGE_ROWS, self::PAGE_NUM_SIZE, false, true);
        }
        $data['is_sh_hr'] =0;
        $hr_auth= $this->apf->get_config('hr_auth','auth');
        if(in_array($_SESSION['user_code'],$hr_auth)){
            $data['is_sh_hr']=1;
        }

        $this->request->set_attribute('params', $uri_params);
        $this->request->set_attribute('selectdata', $selectdata);
        $this->request->set_attribute('data', $data);
        return "Hr_Staff_List";
    }
    /**
     * //从职位列表过来的
     * @param int $job_id
     * @return Ambigous <number, unknown>
     */
    private function _build_get_data($job_id){
        if($job_id){
            $params['search_type']=3;
            $job_bll = new Bll_Hr_Job();
            $dep_bll = new Bll_Hr_Department();
            $jobdata=$job_bll->find_by_job_id($job_id);
            if(!empty($jobdata)){
                $dep_data = $dep_bll->find_dep_by_id($jobdata['department_id']);
                $params['function_id'] = $dep_data['function_id'];
                $params['kw'] = $jobdata['position_name'];
                $params['jobid'] = $job_id;
                return $params;
            }
        }
    }
    /**
     * 从离职转岗过来的信息
     * @param unknown_type $params
     * @return multitype:number string
     */
    private function _build_user_data($params){
        $data = array();
        $parent_id = $params['parent_id'];
        if(empty($parent_id)){
            return $data;
        }
        //get work
        $workinfo = $this->user_bll->get_user_work($parent_id);
        $job_id = $workinfo['job_id'];
        //
        $rel_bll = new Bll_Hr_UserRelation();
        $sub_user = $rel_bll->get_user_sub($parent_id,$job_id);
        if(empty($sub_user)){
            return $data;
        }
        foreach($sub_user as $val){
            $data[] = $val['user_id'];
        }
        return $data;
    }
    /**
     * search
     * @param unknown_type $params
     * @return multitype:number string
     */
    private function _build_where($params){
        $_where = array();
        if(!empty($params['city_id'])){
            $_where['city_id']=intval($params['city_id']);
        }
        if(!empty($params['bu_id'])){
            $_where['bu_id']=intval($params['bu_id']);
        }
        if(!empty($params['function_id'])){
            $_where['function_id']=intval($params['function_id']);
        }
        if(!empty($params['kw'])){
            $_where['kw']=trim($params['kw']);
        }
        if(!empty($params['jobid'])){
            $_where['jobid']=trim($params['jobid']);
        }
        if(!empty($params['parent_id'])){
            $_where['parent_id']=trim($params['parent_id']);
        }
        $_where['search_type']= empty($params['search_type']) ? 1 : intval($params['search_type']);
        $_where['job_status']=1;
        return $_where;
    }
}